/**
 * MiniGolf Scorecard
 * Copyright (C) 2010  MiriTheQuiet, http://minigolfscorecard.googlecode.com/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cz.mtq.mgsc;

import java.util.Hashtable;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import cz.mtq.mgsc.model.Players;
import cz.mtq.mgsc.view.GameOptionsScreen;

public class MgscMidlet extends MIDlet implements CommandListener {

	private final Players players = new Players();

	private final Hashtable commands = new Hashtable(4);
	
	private boolean initialized = false;
	
	protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
	}

	protected void pauseApp() {
	}

	protected void startApp() throws MIDletStateChangeException {
		// com.sun.lwuit.Display.init(this);
		Settings.setResoureBundleLocale(Settings.getDefaultLocale(this));
		if (!initialized){
			new GameOptionsScreen(this).setPlayersScreen();
			initialized = true;
		}
	}

	public void addCommand(Displayable displayable, cz.mtq.mgsc.command.AbstractCommand cmd) {
		Command command = cmd.getCommand();
		// add command to displayable
		displayable.addCommand(command);
		// save the command for later execution
		this.commands.put(command, cmd);
	}

	public void removeCommand(Displayable displayable, cz.mtq.mgsc.command.AbstractCommand cmd) {
		Command command = cmd.getCommand();
		this.commands.remove(command);
		displayable.removeCommand(command);
	}

	public cz.mtq.mgsc.command.AbstractCommand getCommand(Command command) {
		return (cz.mtq.mgsc.command.AbstractCommand) commands.get(command);
	}

	public void commandAction(Command c, Displayable d) {
		cz.mtq.mgsc.command.AbstractCommand cmd = getCommand(c);
		if (cmd != null) {
			cmd.setDisplayable(d);
			try {
				cmd.execute();
			} catch (Exception e) {
				// TODO show an alert?
				e.printStackTrace();
			}
		}
	}

	
	public void setCurrent(Displayable displayable) {
		//TODO replaced current must clean resources! 
		Display.getDisplay(this).setCurrent(displayable);
	}

	public Players getPlayers() {
		return players;
	}

	public void sortPlayers() {
		players.sortPlayers();
	}

	public void callSerially(Runnable thread) {
		Display.getDisplay(this).callSerially(thread);
	}
}
